package com.xmut.mall.member.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xmut.mall.member.utils.RespBean;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.member.utils.PageUtils;
import com.xmut.mall.member.utils.Query;

import com.xmut.mall.member.dao.MemberReceiveAddressDao;
import com.xmut.mall.member.entity.MemberReceiveAddressEntity;
import com.xmut.mall.member.service.MemberReceiveAddressService;
import org.springframework.transaction.annotation.Transactional;


@Service("memberReceiveAddressService")
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressDao, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberReceiveAddressEntity> page = this.page(
                new Query<MemberReceiveAddressEntity>().getPage(params),
                new QueryWrapper<MemberReceiveAddressEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 用户添加收货地址
     * @param address
     * @return
     */
    @Override
    public RespBean addUserReceive(MemberReceiveAddressEntity address) {
        address.setCreateTime(new Date());
        boolean save = this.save(address);
        if(save){
            return RespBean.success("添加成功!");
        }else {
            return RespBean.error("添加失败网络延迟");
        }
    }

    /**
     * 根据用户id获取用户的收货地址
     * @param userId
     * @return
     */
    @Override
    public List<MemberReceiveAddressEntity> getUserReceiveByUserId(Long userId) {

        return this.list(new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id",userId));
    }

    /**
     * 根据id获取用户收货地址
     * @param id
     * @return
     */
    @Override
    public MemberReceiveAddressEntity getReceiveAddressById(Long id) {
        return this.getById(id);
    }

    /**
     * 修改用户收货地址的详细数据
     * @param address
     * @return
     */
    @Override
    public Boolean updateUserReceiveAddress(MemberReceiveAddressEntity address) {
        int i = this.baseMapper.updateById(address);
        if(i!=0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 获取用户的默认收货地址
     * @param id
     * @return
     */
    @Override
    public MemberReceiveAddressEntity getDefaultReceiveAddressById(Long id) {
        return this.getOne(new QueryWrapper<MemberReceiveAddressEntity>()
                .eq("member_id",id)
                .eq("default_status",1));
    }

    /**
     * 删除用户的收货地址
     * @param id
     * @return
     */
    @Override
    public RespBean deleteUserReceiveAddress(Long id) {
        boolean b = this.removeById(id);
        if (b){
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }

    }

    /**
     * 设置默认收货地址
     * @param params
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public RespBean setDefaultAddress(Map<String, Object> params) {
        Long id = MapUtil.getLong(params, "id");
        Long userId = MapUtil.getLong(params, "userId");
        Boolean defaultStatus = MapUtil.getBool(params, "defaultStatus");
        boolean flag = false;
        if (defaultStatus==true){
            // 如果是改为默认地址 需要先查出来用户的全部地址然后设置为不可用 然后再将目标设置为默认
            this.update(new UpdateWrapper<MemberReceiveAddressEntity>()
                    .set("default_status",false)
                    .eq("member_id",userId));
            // 将目标改为默认地址
            flag =  this.update(new UpdateWrapper<MemberReceiveAddressEntity>()
                    .set("default_status",true)
                    .eq("id",id));
        }else {
            // 如果是取消默认地址 直接修改
            flag = this.update(new UpdateWrapper<MemberReceiveAddressEntity>()
                    .set("default_status",false)
                    .eq("id",id));
        }

        if (flag){
            return RespBean.success("修改成功");
        }else {
            return RespBean.error("修改失败");
        }

    }

}